﻿@model FeaturesViewModel
@using Orchard.Localization;
@using Orchard.Modules.Extensions;
@using Orchard.Modules.ViewModels;
@using Orchard.Utility.Extensions;
@using Orchard.Modules.Models;

@{
    Style.Require("ModulesAdmin");
    Style.Require("Switchable");
    Script.Require("Switchable");

    Layout.Title = T("Modules").ToString();
}

@if (Model.Features.Count() > 0) {
<ul class="features summary-view switchable">@{
    var featureGroups = Model.Features.OrderBy(f => f.Descriptor.Category, new DoghouseComparer("Core")).GroupBy(f => f.Descriptor.Category);
    foreach (var featureGroup in featureGroups) {
        var categoryName = LocalizedString.TextOrDefault(featureGroup.First().Descriptor.Category, T("Uncategorized"));
        var categoryClassName = string.Format("category {0}", Html.Encode(categoryName.ToString().HtmlClassify()));
        if (featureGroup == featureGroups.First()) {
            categoryClassName += " first";
        }
        if (featureGroup == featureGroups.Last()) {
            categoryClassName += " last";
        }

        bool showEnable, showDisable;
    <li class="@categoryClassName">
        <h2>@categoryName</h2>
        <ul>@{
            var features = featureGroup.OrderBy(f => f.Descriptor.Name);
            foreach (var feature in features) {
                //hmmm...I feel like I've done this before...
                var featureId = feature.Descriptor.Id.AsFeatureId(n => T(n));
                var featureName = string.IsNullOrEmpty(feature.Descriptor.Name) ? feature.Descriptor.Id : feature.Descriptor.Name;
                var featureState = feature.IsEnabled ? "enabled" : "disabled";
                var featureClassName = string.Format("feature {0}", featureState + (feature.NeedsUpdate ? " update" : String.Empty));
                if (feature == features.First()) {
                    featureClassName += " first";
                }
                if (feature == features.Last()) {
                    featureClassName += " last";
                }

                if (feature.IsRecentlyInstalled) {
                    featureClassName += " recentlyInstalledFeature";
                }

                var dependencies = (from d in feature.Descriptor.Dependencies
                                    select (from f in Model.Features where f.Descriptor.Id.Equals(d, StringComparison.OrdinalIgnoreCase) select f).SingleOrDefault()).Where(f => f != null).OrderBy(f => f.Descriptor.Name);
                var missingDependencies = feature.Descriptor.Dependencies
                    .Where(d => !Model.Features.Any(f => f.Descriptor.Id.Equals(d, StringComparison.OrdinalIgnoreCase)));
                showDisable = categoryName.ToString() != "Core";
                showEnable = !missingDependencies.Any() && feature.Descriptor.Id != "Orchard.Setup";
            <li class="@featureClassName" id="@featureId" title="@T("{0} is {1}", Html.AttributeEncode(featureName), featureState)">
                <div class="summary">
                    <div class="properties">
                        <h3>@featureName</h3>
                        <p class="description">@feature.Descriptor.Description</p>
                        @if (feature.Descriptor.Dependencies != null && feature.Descriptor.Dependencies.Any()) {
                        <div class="dependencies">
                            <h4>@T("Depends on:")</h4>
                            @Html.UnorderedList(dependencies,
                            (s, i) => Html.Link(string.IsNullOrEmpty(s.Descriptor.Name) ? s.Descriptor.Id : s.Descriptor.Name, string.Format("#{0}", s.Descriptor.Id.AsFeatureId(n => T(n)))),
                        "",
                        "dependency",
                        "")
                        </div>}
                        @if (missingDependencies.Any()) {
                        <div class="missingdependencies">
                            <h4>@T("Missing:")</h4>
                            @Html.UnorderedList(missingDependencies, (s, i) => MvcHtmlString.Create(s), "", "missingdependency", "")
                        </div>}
                    </div>
                    <div class="actions">
                        @if (showDisable && feature.IsEnabled) {
                            using (Html.BeginFormAntiForgeryPost(string.Format("{0}", Url.Action("Disable", new { area = "Orchard.Modules" })), FormMethod.Post, new {@class = "inline link"})) {
                                @Html.Hidden("id", feature.Descriptor.Id, new {id = ""})
                                @Html.Hidden("force", true)
                                <button type="submit">@T("Disable")</button> 
                            }
                        }

                        @if(showEnable && !feature.IsEnabled) {
                            using (Html.BeginFormAntiForgeryPost(string.Format("{0}", Url.Action("Enable", new { area = "Orchard.Modules" })), FormMethod.Post, new {@class = "inline link"})) {
                                @Html.Hidden("id", feature.Descriptor.Id, new { id = "" })
                                @Html.Hidden("force", true)
                                <button type="submit">@T("Enable")</button> 
                            }
                        }

                        @if(feature.NeedsUpdate){
                                using (Html.BeginFormAntiForgeryPost(string.Format("{0}", Url.Action("Update", new { area = "Orchard.Modules" })), FormMethod.Post, new {@class = "inline link"})) {
                                @Html.Hidden("id", feature.Descriptor.Id, new { id = "" })
                                <button type="submit" class="update">@T("Update")</button> 
                            }
                        }
                    </div>
                </div>
            </li>}
        }</ul>
    </li>}
}</ul>}
      
@using(Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
    $(function () {
        var searchBox = $("<fieldset class=\"bulk-actions\"><label for=\"search-box\">@T("Filter:")</label> <input id=\"search-box\" class=\"text-box\" type=\"text\" /></fieldset>");
        var theSwitch = $(".switch-for-switchable");
        theSwitch.prepend(searchBox);
        $("#search-box").focus().keyup(function () {
            var text = $(this).val();

            if (text == '') {
                $("li.category").show();
                $("li.feature:hidden").show();
                return;
            }

            $("li.feature").each(function () {
                var elt = $(this);
                var value = elt.find('h3:first').text();
                if (value.toLowerCase().indexOf(text.toLowerCase()) >= 0)
                    elt.show();
                else
                    elt.hide();
            });

            $("li.category:hidden").show();
            var toHide = $("li.category:not(:has(li.feature:visible))").hide();
        });
    })
//]]>
</script>
}